package TestHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.impl.client.DefaultHttpClient;

public class MsSwiftFirst {

	public static String host = "www.swift.com";
	public static String swiftServer = "kftest4.sqa.cm4.tbsite.net";
	public static String uriPrefix = "http://";

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
	      // TODO Auto-generated method stub
	      HttpClient httpclient = new DefaultHttpClient();
	      String obj = "/swiftdebug/1";
	      String longuri = "a";
	      for(int i = 1; i < 13; i ++)
	    	  longuri += longuri;
	      
	      String tmpuri = uriPrefix + swiftServer + obj + longuri + longuri;
	      String uri = tmpuri.substring(0, 7700);
	      System.out.println(uri.length());
	      
	      String longhost = "a";
	      for(int i = 1; i < 14; i ++)
	    	  longhost += longhost;
	      
	      String tony = "b";
	      for(int k = 1; k < 14; k ++)
	    	  tony += tony;
	      //longhost = longhost + tony + tony;
	      longhost = tony.substring(0, 7600);
	      System.out.println(longhost.length());
	      
	      HttpGet httpget = new HttpGet(uri);
	      httpget.addHeader("Host", host);
	      System.out.println(httpget.getRequestLine());
	      Header[] requestHeader = httpget.getAllHeaders();
	      for(Header header : requestHeader )
	    	  System.out.println(header);
	      
	      HttpResponse response = httpclient.execute(httpget);
	      StatusLine statusline = response.getStatusLine();
	      System.out.println(statusline);
	      
	      Header[] allHeader = response.getAllHeaders();
	      for(Header header : allHeader )
	    	  System.out.println(header);
	      
	      HttpEntity httpentity = response.getEntity();
	      System.out.println(httpentity.getContentEncoding());
	      
	      if(httpentity != null) {
	         InputStream instream = httpentity.getContent();
	         try {
	            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
	            System.out.println(reader.readLine());
	         } catch (IOException ex) {
	            throw ex;
	         } catch (RuntimeException ex) {
	            httpget.abort();
	            throw ex;
	         }
	         finally {
	            instream.close();
	         }
	         httpclient.getConnectionManager().shutdown();
	      }
	      
	      }
	   public static boolean checkResponseCode() throws Exception {
		      // TODO Auto-generated method stub
		      HttpClient httpclient = new DefaultHttpClient();
		      HttpTrace httptrace = new HttpTrace("http://www.baidu.com");
		      //HttpRequest httprequest = new BasicHttpRequest("Get", "http://www.baidu.com");
		      //RequestWrapper requestwapper = new RequestWrapper(httprequest);
		      HttpResponse response = httpclient.execute(httptrace);
		      StatusLine statusline = response.getStatusLine();
		      System.out.println(statusline);
		      System.out.println(statusline.getProtocolVersion() + " " + statusline.getReasonPhrase());
		      HttpEntity httpentity = response.getEntity();
		      System.out.println(httpentity.getContentEncoding());
		      
		      if(httpentity != null) {
		         InputStream instream = httpentity.getContent();
		         try {
		            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
		            System.out.println(reader.readLine());
		         } catch (IOException ex) {
		            throw ex;
		         } catch (RuntimeException ex) {
		            httptrace.abort();
		            throw ex;
		         }
		         finally {
		            instream.close();
		         }
		         httpclient.getConnectionManager().shutdown();
		      }
		      return true;
		   }

	   
	   
}
